package leetcode_800;


/**
 *@author 周杨
 *FindPivotIndex_724 问一个数组是否能用index划分 左半部分和右半部分元素和相等
 *describe:用数组和 AC 64%
 *2018年10月12日 下午4:24:51
 */
public class FindPivotIndex_724 {
	public int pivotIndex(int[] nums) {
		    int sum = 0;
	        int res = -1;
		// the sum of the array
	        for (int i : nums) {
	            sum += i;
	        }
	        int leftTemp = 0; // the sum of array which left of the num
	        int rightTemp = sum; // the sum of array which right of the num
	        for (int m = 0; m < nums.length; m++) {
		// the handle the head num
	           if (m == 0) {
	                leftTemp = 0;
	                rightTemp = sum-nums[m];
	            } else {
	                leftTemp += nums[m-1];
	                rightTemp -= nums[m];
	            } 
	            if (leftTemp == rightTemp) {
	                res = m;
	                break;
	            } else if (m != nums.length-1) {
	                continue;
	            } else if (m == nums.length-1) {
	                break;
	            }
	        }
	        return res;
    }
}
